#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author： 胡浩浩
# datetime： 2021/12/28 15:46 
# ide： PyCharm
from sqlalchemy.orm import Session
from Models.userModel import user_model
from Schemas.userSchema import user_schema


def get_user_by_id(db: Session, user_id: int):
    return db.query(user_model.User).filter(user_model.User.id == user_id).first()


def get_user_list(db: Session, skip: int = 0, limit: int = 10):
    return db.query(user_model).offset(skip).limit(limit).all()


def create_user(db: Session, user: user_schema.UserCreate):
    db_user = user_schema.User(**user.dict())
    db.add(db_user)
    db.commit()
    db.refresh(db_user)
    return db_user


def get_group_by_id(db: Session, group_id: int):
    return db.query(user_model.Group).filter(user_model.Group.id == group_id).first()


def get_group_list(db: Session, skip: int = 0, limit: int = 10):
    return db.query(user_model.Group).offset(skip).limit(limit)


def create_group(db: Session, group: user_schema.GroupCreate):
    db_group = user_model.Group(**group.dict())
    db.add(db_group)
    db.commit()
    db.refresh(db_group)
    return db_group


def get_user_group_relation_by_id(db: Session, relation_id: int):
    return db.query(user_model.UserGroupRelation).filter(user_model.UserGroupRelation.id == relation_id).first()


def get_user_group_relation_by_user_id(db: Session, user_id: int):
    return db.query(user_model.UserGroupRelation).filter(user_model.UserGroupRelation.user_id == user_id).first()


def get_user_group_relation_by_group_id(db: Session, group_id: int):
    return db.query(user_model.UserGroupRelation).filter(user_model.UserGroupRelation.group_id == group_id).first()


def create_user_group_relation(db: Session, relation: user_schema.UserGroupRelationCreate):
    db_relation = user_model.UserGroupRelation(**relation.dict())
    db.add(db_relation)
    db.commit()
    db.refresh(db_relation)
    return db_relation
